package com.tydic.enterprise.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tydic.common.core.domain.model.LoginUser;
import com.tydic.common.core.enums.UserCertificationStatusEnum;
import com.tydic.common.satoken.utils.LoginHelper;
import com.tydic.enterprise.domain.Enterprise;
import com.tydic.enterprise.domain.EnterpriseUser;
import com.tydic.enterprise.mapper.EnterpriseUserMapper;
import com.tydic.enterprise.service.IEnterpriseService;
import com.tydic.enterprise.service.IEnterpriseUserService;
import com.tydic.system.domain.SysUser;
import com.tydic.system.mapper.SysUserMapper;
import com.tydic.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class EnterpriseUserServiceImpl implements IEnterpriseUserService {
    private final ISysUserService sysUserService;
    private final IEnterpriseService enterpriseService;
    private final EnterpriseUserMapper enterpriseUserMapper;
    private final SysUserMapper sysUserMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(SysUser sysUser, List<String> roleCodes) {
        LoginUser user = LoginHelper.getLoginUser();
        Enterprise enterprise = enterpriseService.getEnterpriseByUserCode(user.getUserCode());
        EnterpriseUser enterpriseUser = new EnterpriseUser();
        enterpriseUser.setId(IdUtil.getSnowflakeNextIdStr());
        enterpriseUser.setEnterpriseId(enterprise.getId());
        enterpriseUser.setUserCode(user.getUserCode());
        enterpriseUser.setCertificationStatus(UserCertificationStatusEnum.READY.getStatus());
        sysUserService.saveUser(sysUser, roleCodes);
        enterpriseUserMapper.insertOrUpdate(enterpriseUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysUser sysUser, List<String> roleCodes) {
        sysUserService.saveUser(sysUser, roleCodes);
    }

    @Override
    public List<EnterpriseUser> findByEnterpriseId(String enterpriseId) {
        return enterpriseUserMapper.selectList(Wrappers.lambdaQuery(EnterpriseUser.class).eq(EnterpriseUser::getEnterpriseId, enterpriseId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByUserCode(String userCode) {
        enterpriseUserMapper.delete(Wrappers.lambdaQuery(EnterpriseUser.class).eq(EnterpriseUser::getUserCode, userCode));
        sysUserMapper.deleteById(userCode);
    }

    @Override
    public EnterpriseUser findByUserCode(String userCode) {
        return enterpriseUserMapper.selectOne(Wrappers.lambdaQuery(EnterpriseUser.class).eq(EnterpriseUser::getUserCode, userCode), false);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(EnterpriseUser enterpriseUser) {
        enterpriseUserMapper.updateById(enterpriseUser);
    }
}
